package com.zhongxi.guoyuan.view.personalCenter;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.CollectionAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.user.center.CollectionData;
import com.zhongxi.guoyuan.entity.user.center.CollectionResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.beautifulStore.ProjectDetailActivity;
import com.zhongxi.guoyuan.view.coinShopping.CoinGoodsActivity;
import com.zhongxi.guoyuan.view.coinShopping.CoinShoppingDetailActivity;
import com.zhongxi.guoyuan.view.rewardCoin.RewardCoinDetailActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyCollectionActivity extends BaseActivity implements View.OnClickListener, OnRefreshListener2<ListView> {

    private PullToRefreshListView lvList;
    private Spinner spinner;

    private List<CollectionData> data = new ArrayList<CollectionData>();
    private int type = 1;
    private CollectionAdapter adapter;
    private CollectionResult result;
    private Map<String, String> map = new HashMap<>();
    private String flag;
    private TextView tvTips;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_my_collection);
        initTitle();
        tvTitle.setText("我的收藏");
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
        tvTips = (TextView) findViewById(R.id.tvTips);

        //智能排序
        order();
    }

    private void order() {
        //智能排序
        spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> spAdapter = new ArrayAdapter<String>(context, R.layout.simple_spinner_item);
        List<String> spList = new ArrayList<String>();
        spList.add("美币项目");
        spList.add("返银商品");
        spList.add("银币商品");
        spList.add("银币商家");
        spAdapter.addAll(spList);
        spAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                try{
                    pageNum = 1;
                    type = position + 1;
                    switch (type){
                        case 1:
                            tvTips.setText("美币项目");
                            break;
                        case 2:
                            tvTips.setText("返银商品");
                            break;
                        case 3:
                            tvTips.setText("银币商品");
                            break;
                        case 4:
                            tvTips.setText("银币商家");
                            break;
                    }
                    getCollectionData();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
    }

    private void getCollectionData() {
        flag = "list";
        String str = "&ftype="+type+"&page="+pageNum;
        getServer(Url.MY_FAVORITE_LIST, null, str, false, null, "get");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        lvList.setOnRefreshListener(this);
        //长按删除
        ListView listView = lvList.getRefreshableView();
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                LayoutInflater inflater = LayoutInflater.from(context);
                View layout = inflater.inflate(R.layout.dialog_del, null);
                final AlertDialog dialog = new AlertDialog.Builder(context).create();
                dialog.show();
                dialog.getWindow().setContentView(layout);
                TextView tvSure = (TextView) layout.findViewById(R.id.tvSure);
                TextView tvCancle = (TextView) layout.findViewById(R.id.tvCancle);

                //确定删除
                tvSure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        map.put("favid", result.getData().get(position-1).getId());
                        map.put("favtype", type+"");
                        flag = "del";
                        getServer(Url.DEL_FAVORITE, map, "", false, null, "post");
                        dialog.dismiss();
                    }
                });

                //确定取消
                tvCancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                return true;
            }
        });
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                switch (type){
                    case 1:
                        //跳转到美币项目页面
                        intent.setClass(context, ProjectDetailActivity.class);
                        intent.putExtra("id", result.getData().get(position-1).getData().getId());
                        startActivity(intent);
                        break;
                    case 2:
                        //跳转到返银币页面
                        intent.setClass(context, RewardCoinDetailActivity.class);
                        intent.putExtra("id", result.getData().get(position-1).getData().getId());
                        startActivity(intent);
                        break;
                    case 3:
                        //跳转到银币消费商品页面
                        intent.setClass(context, CoinGoodsActivity.class);
                        intent.putExtra("goodsId", result.getData().get(position-1).getData().getId());
                        startActivity(intent);
                        break;
                    case 4:
                        //跳转到银币消费商家页面
                        intent.setClass(context, CoinShoppingDetailActivity.class);
                        intent.putExtra("shopid", result.getData().get(position-1).getData().getId());
                        startActivity(intent);
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
        }
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            if("list".equals(flag)){
                result = gson.fromJson(response, CollectionResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if(pageNum==1){
                    data.clear();
                }
                if(result.getData().size()>0){
                    data.addAll(result.getData());
                }
                lvList.onRefreshComplete();
                adapter = new CollectionAdapter(context, data);
                lvList.setAdapter(adapter);
            }else if("del".equals(flag)){
                BaseResponse result = gson.fromJson(response, BaseResponse.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                getCollectionData();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum=1;
        getCollectionData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum++;
        getCollectionData();
    }
}
